package com.weilive.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.weilive.core.utils.FileUtils;
import com.weilive.core.utils.PropertiesFile;

/**
 * 常量配置类
 * 
 * @author houzhiwei
 *
 */
public class ServerConfig {

	private static final Logger logger = LoggerFactory.getLogger(ServerConfig.class);

	private ServerConfig() {
	}

	private static final String PROPERTIES_FILE = "system.properties";
	private static final int WORKING_ENV = 1;
	// 工作目录
	private static String workPath;
	// 资源路径
	private static String resourcePath = "/SResource/";
	// js资源路径
	private static String scriptPath = "/SResource/javascript/";
	// 环境
	private static int env = 0;
	// cache过期时间 分钟
	private static int cacheTimeout = 60 * 1000;
	// 直播显示最大人数
	private static int liveMaxcount = 100;
	// 发现显示最大人数
	private static int discoverMaxcount = 240;
	// 达人显示最大
	private static int dorenShowMaxcount = 12;
	// 首页视频显示最大
	private static int liveShowMaxcount = 6;
	// 发现推荐显示最大
	private static int discoverShowMaxcount = 3;
	// 简介最长
	private static int userProfileLen = 20;
	// 用户名字最长
	private static int userAliasLen = 25;
	// 分页最大值
	private static int pageMaxcount = 30;
	// 关注最大
	private static int pageGuanzhuMaxcount = 30;
	// 数据load间隔时间,单位秒
	private static int timerPeriod = 5;
	// 预告过期消失时间,单位秒
	private static int videoTimeExpire = 300;
	// 线程池数量
	private static int timerPoolSize = 1;
	// 直播推流地址
	private static String liveUrl = "http://hlsstream.weilive.cc/live/%s/playlist.m3u8";
	// 金山buckeyName
	private static String ks3BuckeyName = "weilive";
	// 金山照片地址
	private static String ks3PhotoURL = "http://%s.kssws.ks-cdn.com/photo/%s";

	// 网宿buckeyName
	private static String wcsBuckeyName = "weilive-video";
	// 网宿秒级禁播url
	private static String wcsReqURL = "http://cm.chinanetcenter.com/CM/cm-command.do?";
	// 网宿秒级禁播用户名
	private static String wcsReqUserName = "weilive";
	// 网宿秒级禁播密码
	private static String wcsReqUserPass = "Weilive123";
	// 网宿推流地址
	private static String wcsReqUpstreamURL = "rtmp://upstream.weilive.cc/live/";

	public static final String getLiveURL(long userId) {
		return String.format(liveUrl, userId);
	}

	private static void setWorkPath(String workPath) {
		ServerConfig.workPath = workPath;
	}

	/**
	 * 加载资源
	 * 
	 */
	public static void loadData(String workPath) {
		// 设置工作目录
		setWorkPath(workPath);
		// 从system.properties文件中加载资源
		reloadData();
	}

	public static void reloadData() {
		PropertiesFile properties = new PropertiesFile(FileUtils.getResourcePath(PROPERTIES_FILE));
		ServerConfig.resourcePath = workPath + properties.get("bean.resourcePath", "/SResource/");
		ServerConfig.scriptPath = workPath + properties.get("bean.scriptPath", "/SResource/javascript/");
		ServerConfig.env = properties.getInt("ENV", 0);
		ServerConfig.cacheTimeout = properties.getInt("cache.timeout", 60 * 1000);
		ServerConfig.liveMaxcount = properties.getInt("live.maxcount", 100);
		ServerConfig.discoverMaxcount = properties.getInt("discover.maxcount", 240);
		ServerConfig.dorenShowMaxcount = properties.getInt("doren.show.maxcount", 12);
		ServerConfig.liveShowMaxcount = properties.getInt("live.show.maxcount", 6);
		ServerConfig.discoverShowMaxcount = properties.getInt("discover.show.maxcount", 3);
		ServerConfig.userProfileLen = properties.getInt("user.profile.len", 20);
		ServerConfig.userAliasLen = properties.getInt("user.alias.len", 25);
		ServerConfig.pageMaxcount = properties.getInt("page.maxcount", 30);
		ServerConfig.pageGuanzhuMaxcount = properties.getInt("page.guanzhu.maxcount", 30);
		ServerConfig.timerPeriod = properties.getInt("timer.period", 5);
		ServerConfig.videoTimeExpire = properties.getInt("video.time.expire", 3) * 60;
		ServerConfig.timerPoolSize = properties.getInt("timer.pool.size", 2);
		ServerConfig.liveUrl = properties.get("live.url", "http://hlsstream.weilive.cc/live/%s/playlist.m3u8");

		// 金山
		ServerConfig.ks3BuckeyName = properties.get("ks3.buckey.name", "weilive");
		ServerConfig.ks3PhotoURL = properties.get("ks3.photo.url", "http://%s.kssws.ks-cdn.com/photo/%s");
		// 网宿
		ServerConfig.wcsBuckeyName = properties.get("wcs.buckey.name", "weilive-video");
		ServerConfig.wcsReqURL = properties.get("wcs.req.url", "http://cm.chinanetcenter.com/CM/cm-command.do?");
		ServerConfig.wcsReqUserName = properties.get("wcs.req.username", "weilive");
		ServerConfig.wcsReqUserPass = properties.get("wcs.req.userpass", "Weilive123");
		ServerConfig.wcsReqUpstreamURL = properties.get("wcs.req.upstream.url", "rtmp://upstream.weilive.cc/live/");

		logger.info("env={},ks3BuckeyName={},wcsBuckeyName={}",
				new Object[] { ServerConfig.env, ServerConfig.ks3BuckeyName, ServerConfig.wcsBuckeyName });
	}

	public static boolean isWorkingEnv() {
		return env == WORKING_ENV;
	}

	public static String getScriptPath() {
		return scriptPath;
	}

	public static String getWorkPath() {
		return workPath;
	}

	public static String getResourcePath() {
		return resourcePath;
	}

	public static int getCacheTimeout() {
		return cacheTimeout;
	}

	public static int getLiveMaxcount() {
		return liveMaxcount;
	}

	public static int getDiscoverMaxcount() {
		return discoverMaxcount;
	}

	public static int getDorenShowMaxcount() {
		return dorenShowMaxcount;
	}

	public static int getLiveShowMaxcount() {
		return liveShowMaxcount;
	}

	public static int getDiscoverShowMaxcount() {
		return discoverShowMaxcount;
	}

	public static int getUserProfileLen() {
		return userProfileLen;
	}

	public static int getUserAliasLen() {
		return userAliasLen;
	}

	public static int getPageMaxcount() {
		return pageMaxcount;
	}

	public static int getPageGuanzhuMaxcount() {
		return pageGuanzhuMaxcount;
	}

	public static int getTimerPeriod() {
		return timerPeriod;
	}

	public static int getVideoTimeExpire() {
		return videoTimeExpire;
	}

	public static String getLiveUrl() {
		return liveUrl;
	}

	public static int getTimerPoolSize() {
		return timerPoolSize;
	}

	public static String getKs3BuckeyName() {
		return ks3BuckeyName;
	}

	public static String getKs3PhotoURL() {
		return ks3PhotoURL;
	}

	public static String getWcsBuckeyName() {
		return wcsBuckeyName;
	}

	public static String getWcsReqURL() {
		return wcsReqURL;
	}

	public static String getWcsReqUserName() {
		return wcsReqUserName;
	}

	public static String getWcsReqUserPass() {
		return wcsReqUserPass;
	}

	public static String getWcsReqUpstreamURL() {
		return wcsReqUpstreamURL;
	}

}
